/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.hnilica.cisteniulic.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.hnilica.cisteniulic.dao.UliceDAO;
import net.hnilica.cisteniulic.dto.Ulice;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
/**
 *
 * @author swosh
 */
public class NotWatchedUliceServ extends HttpServlet {
    private static final String url = "/CisteniUlic/sprava/index.jspx?akce=sprava";
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String search = request.getParameter("search");
        Subject subject = SecurityUtils.getSubject();
                //principal is sam as an email in database
        String principal = (String) subject.getPrincipal();
        UliceDAO uliceDAO = new UliceDAO();
        Collection<Ulice> collUlice = uliceDAO.getBySearch(principal, search);
        StringBuffer sb = new StringBuffer();
        boolean first = true;

        PrintWriter out = response.getWriter();
        try {
            for (Iterator<Ulice> it = collUlice.iterator(); it.hasNext();) {
                Ulice ulice = it.next();
                if (first) {
                    first = false;
                    out.println("<ul>");
                }
                out.println("<li class='search'>");
                out.println(ulice.getNazUlice());
                out.println("</li>");
                if(it.hasNext() == false) {
                    out.println("</ul>");
                }
            }
        } finally {
            out.flush();
            out.close();
        }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
